package org.example.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWTUtil;
import org.example.domain.CarClient;
import org.example.domain.CarClientPlan;
import org.example.domain.CarPlan;
import org.example.service.CarClientPlanService;
import org.example.service.CarClientService;
import org.example.service.CarPlanService;
import org.example.util.Result;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("carClientPlan")
public class CarClientPlanController {
    @Autowired
    CarClientPlanService carClientPlanService;
    @Autowired
    CarClientService carClientService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    CarPlanService carPlanService;
    @Autowired
    RedissonClient redissonClient;
    @RequestMapping("saveClientPlan")
    public Result  saveClientPlan(@RequestBody CarPlan carPlan){
        carPlan.setCreateTime(new Date());
        // 检查客户状态
        String token = request.getHeader("token");
        Integer clientId = (Integer) JWTUtil.parseToken(token).getPayload("clientId");
        System.err.println(clientId);
        CarClient carClient = carClientService.selectByClientId(clientId);
        String status = carClient.getStatus();
        if ("1".equals(status)) { // 假设状态为字符串类型，应该用.equals()比较
            return Result.fail("每一个客户只能抢购一张");
        }
        boolean b = carClientPlanService.saveClientPlan(carPlan);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("selectByPlanId")
    public Result selectByPlanId(Integer clientId){
        List<CarPlan> list = carClientPlanService.selectByPlanId(clientId);
        return Result.success(list);
    }
    @RequestMapping("QiangDan")
    public  Result QiangDan(Integer planId){
        String token = request.getHeader("token");
        Integer clientId = (Integer) JWTUtil.parseToken(token).getPayload("clientId");
        System.err.println("用户使用人"+clientId);
        System.err.println("优惠卷"+planId);
        CarClientPlan carClientPlan = new CarClientPlan();
        carClientPlan.setClientId(clientId);
        carClientPlan.setPlanId(planId);
        DateTime createTime = DateUtil.date();//当前时间
        CarPlan carPlan = carPlanService.selectByPlanById(planId);
        if(!(createTime.after(carPlan.getCreateTime()) && createTime.before(carPlan.getUpdateTime()))){
            return Result.fail("优惠卷已经过期了，不可以抢");
        }
        if(carPlan.getTotalNum()<=0){
            return Result.fail("库存不足不可以进行抢购");
        }
        CarPlan car = carPlanService.selectByplanIdAndClientId(planId,clientId);
        if(null!=car){
            return Result.fail("你已经抢个这个优惠卷不能再抢了");
        }
        synchronized (this){
            //用户抢单
            carClientPlanService.QiangDan(carClientPlan);
            //扣除数量
            carPlanService.updateQiangDanById(planId);
        }
        return Result.success();
    }

    @RequestMapping("QiangDan2")
    public  Result QiangDan2(Integer planId){
        Integer clientId = 1;
        CarClientPlan carClientPlan = new CarClientPlan();
        carClientPlan.setClientId(clientId);
        carClientPlan.setPlanId(planId);
        DateTime createTime = DateUtil.date();//当前时间
        CarPlan carPlan = carPlanService.selectByPlanById(planId);
        if(!(createTime.after(carPlan.getCreateTime()) && createTime.before(carPlan.getUpdateTime()))){
            return Result.fail("优惠卷已经过期了，不可以抢");
        }
        if(carPlan.getTotalNum()<=0){
            return Result.fail("库存不足不可以进行抢购");
        }
        CarPlan car = carPlanService.selectByplanIdAndClientId(planId,clientId);
        if(null!=car){
            return Result.fail("你已经抢个这个优惠卷不能再抢了");
        }
        synchronized (this){
            //用户抢单
            carClientPlanService.QiangDan(carClientPlan);
            //扣除数量
            carPlanService.updateQiangDanById(planId);
        }
        return Result.success();
    }


}
